#include "oproperty.h"

#include <QBoxLayout>
#include <QTableView>
#include <QTreeView>
#include <QScrollArea>

#include "ocreator.h"
#include "oapplication.h"

OPropertyModel::OPropertyModel(QObject *parent)
	: QAbstractItemModel(parent)
{
	
}

OPropertyModel::~OPropertyModel()
{

}

QVariant OPropertyModel::data(const QModelIndex &index, int role) const
{
	if(!index.isValid())
		return QVariant();
	QList<QByteArray> names = oCreator()->dynamicPropertyNames();
	if(role == Qt::DisplayRole || role == Qt::EditRole){
		int col = index.column();
		int row = index.row();
		return col == 0 ? names.at(row) : oCreator()->property(names.at(row));
	}
	else if(role == Qt::DecorationRole && index.column() == 0){
		if(names.at(index.row()).startsWith("-")){
			return QIcon(":/omainwind/Resources/develop-icon.png");
		}
	}else if(role == Qt::BackgroundRole){
		if(!names.at(index.row()).startsWith("-")){
			return QBrush(Qt::darkGray);
		}
	}else if(role == Qt::ForegroundRole){
		if(!names.at(index.row()).startsWith("-")){
			return QBrush(Qt::white);
		}
	}
	return QVariant();
}

bool OPropertyModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
	if(role == Qt::EditRole && index.isValid() && index.column() == 1){
		QList<QByteArray> names = oCreator()->dynamicPropertyNames();
		oCreator()->setProperty(names.at(index.row()), value);
		emit dataChanged(index, index);
		return true;
	}		
	return false;
}

Qt::ItemFlags OPropertyModel::flags(const QModelIndex &index) const
{
	if(!index.isValid())
		return 0;
	QList<QByteArray> names = oCreator()->dynamicPropertyNames();
	if(index.column() == 0 || (!names.at(index.row()).startsWith("-")))
		return (Qt::ItemIsEnabled | Qt::ItemIsSelectable); 
	return (Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable);
}

QVariant OPropertyModel::headerData(int section, Qt::Orientation orientation, 
							   int role /* = Qt::DisplayRole */) const
{
	if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
		return section == 0 ? tr("Name") : tr("Value");
	return QVariant();
}

QModelIndex OPropertyModel::index(int row, int column, const QModelIndex &parent /* = QModelIndex */) const
{
	if(!hasIndex(row, column, parent))
		return QModelIndex();
	if(parent.isValid())
		return QModelIndex();
	return createIndex(row, column);
}

QModelIndex OPropertyModel::parent(const QModelIndex &index) const
{
	return QModelIndex();
}

int OPropertyModel::rowCount(const QModelIndex &parent) const
{
	if(!parent.isValid())
		return oCreator()->dynamicPropertyNames().size();
	return 0;
}

int OPropertyModel::columnCount(const QModelIndex &parent /* = QModelIndex */) const
{
	return 2;
}

OPropertyDock::OPropertyDock(QWidget* parent /* = 0 */) 
: QDockWidget(tr("Properties"), parent)
{
	QTreeView* vw = new QTreeView;
	vw->setModel(new OPropertyModel(this));
	vw->setAlternatingRowColors(true);
	vw->setAutoScroll(true);
	vw->setIndentation(0);
	//QScrollArea* sa = new QScrollArea;
	//sa->setWidget(vw);
	setWidget(vw);
}

OPropertyDock::~OPropertyDock(){}